package com.third.clock.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.third.clock.R;
import com.third.clock.bean.MediaEntity;
import com.third.clock.bean.NoticeEntity;
import com.third.clock.manager.TransformManager;
import com.third.clock.ui.model.adapter.NoticeDetailsAdapter;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2018/8/10 0010.
 */

public class NoticeDetailsActivity extends Activity implements SwipeItemClickListener {

    private SwipeMenuRecyclerView mSwipeMenuRecyclerView;

    private NoticeDetailsAdapter adapter;

    private View mRecyclerViewHeaderView;

    private TextView mRecyclerViewHeaderViewTitle;

    private TextView mRecyclerViewHeaderViewTime;

    private TextView mRecyclerViewHeaderViewContent;

    private View mHeadView;

    private TextView mHeadViewTitle;

    private NoticeEntity mNoticeEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticedetails);
        initView();
        getIntentData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onItemClick(View itemView, int position) {
         ImageBrowserActivity.intentData(NoticeDetailsActivity.this,mNoticeEntity.getMedias(),position);
    }

    private void initView(){
        mHeadView=findViewById(R.id.head_noticedetails);
        mHeadViewTitle= (TextView) mHeadView.findViewById(R.id.textview_head_title);
        mHeadViewTitle.setText("公告详情");

        mSwipeMenuRecyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recycler_view);
        mSwipeMenuRecyclerView.setSwipeItemClickListener(this);

        mSwipeMenuRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
       // mSwipeMenuRecyclerView.addItemDecoration(new DefaultItemDecoration(ContextCompat.getColor(this, R.color.colorPrimary)));

        // HeaderView。
        mRecyclerViewHeaderView = getLayoutInflater().inflate(R.layout.item_head_noticedetails, mSwipeMenuRecyclerView, false);
        mRecyclerViewHeaderViewTitle= (TextView) mRecyclerViewHeaderView.findViewById(R.id.textview_notice_title);
        mRecyclerViewHeaderViewTime=(TextView) mRecyclerViewHeaderView.findViewById(R.id.textview_notice_time);
        mRecyclerViewHeaderViewContent=(TextView) mRecyclerViewHeaderView.findViewById(R.id.textview_notice_content);

        mSwipeMenuRecyclerView.addHeaderView(mRecyclerViewHeaderView);

        adapter=new NoticeDetailsAdapter(NoticeDetailsActivity.this);
        mSwipeMenuRecyclerView.setAdapter(adapter);

    }



    private void initData(){
       if(mNoticeEntity!=null){
           String title=mNoticeEntity.getTitle();
           if(!TextUtils.isEmpty(title)){
               mRecyclerViewHeaderViewTitle.setText(title);
           }
           String time=mNoticeEntity.getCdate();
           if(!TextUtils.isEmpty(title)){
               try {
                   mRecyclerViewHeaderViewTime.setText(TransformManager.paserLongToStringDate("yyyy-MM-dd HH:mm", Long.parseLong(time) * 1000));
               }catch (Exception e){

               }
           }
           String content=mNoticeEntity.getContent();
           if(!TextUtils.isEmpty(title)){
               mRecyclerViewHeaderViewContent.setText(content);
           }

           List<MediaEntity> list=mNoticeEntity.getMedias();
           if(list!=null){
               adapter.setList(list);
           }

       }

    }

    private void getIntentData(){
        Intent intent=getIntent();
        if(intent!=null&&intent.getExtras()!=null){
            mNoticeEntity= (NoticeEntity) intent.getSerializableExtra("NoticeEntity");
        }
    }

    public static void intentData(Context context, NoticeEntity mNoticeEntity){
        Intent intent = new Intent(context, NoticeDetailsActivity.class);
        intent.putExtra("NoticeEntity", mNoticeEntity);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


}
